package com.ztjava.common.controller;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;

@ControllerClass(path="/common",client=PortalClient.class)
@UserController(portal=Constant.P_PORTAL)
public class CommonPortalController extends BaseController {

	@SuppressWarnings("unchecked")
	@UserMethod("CommonParam")
	public void showParam(){
		Cache cache = Redis.use();
		Map<String,String> map = cache.hgetAll(Constant.R_PARAM);
		Map<String, String> paramMap = sortMapByKey(map);
		setAttr("paramMap", paramMap);
		render("showParam.html");
	}
	 public static Map<String, String> sortMapByKey(Map<String, String> map) {
	        if (map == null || map.isEmpty()) {
	            return null;
	        }

	        Map<String, String> sortMap = new TreeMap<String, String>(
	                new MapKeyComparator());
	        sortMap.putAll(map);

	        return sortMap;
	    }
	@UserMethod("CommonParam")
	public void saveParam(){
		renderError("此功能尚未实现");
	}
}

class MapKeyComparator implements Comparator<String>{

    @Override
    public int compare(String str1, String str2) {

        return str1.compareTo(str2);
    }
}